package com.fc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fc.context.BaseContext;
import com.fc.dto.ExpenseDTO;
import com.fc.dto.pageQuery.ExpensePageQueryDTO;
import com.fc.entity.Expense;
import com.fc.entity.Logistics;
import com.fc.enums.ReviewStatus;
import com.fc.exception.ExpenseException;
import com.fc.mapper.ExpenseMapper;
import com.fc.result.PageResult;
import com.fc.service.IExpenseService;
import com.fc.utils.MonthUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ExpenseServiceImpl extends ServiceImpl<ExpenseMapper, Expense> implements IExpenseService {


    @Override
    public void saveExpense(ExpenseDTO expenseDTO) {
        Expense expense = BeanUtil.copyProperties(expenseDTO, Expense.class);
        expense.setDriverId(BaseContext.getCurrentId());
        expense.setDate(LocalDateTime.now());
        expense.setStatus(0);//待审核
        save(expense);

    }

    @Override
    public void updateExpense(ExpenseDTO expenseDTO) {
        Expense expense = getExpense(expenseDTO.getId());
        if(expense.getStatus()==ReviewStatus.REJECT.getCode()){
            expense.setStatus(ReviewStatus.REVIEWING.getCode());
        }
        BeanUtil.copyProperties(expenseDTO, expense);
        updateById(expense);
    }



    @Override
    public PageResult listExpense(ExpensePageQueryDTO expensePageQueryDTO) {
        Page<Expense> page = new Page<>(expensePageQueryDTO.getPageNum(), expensePageQueryDTO.getPageSize());
        Integer month = expensePageQueryDTO.getMonth();
        boolean flag= month != null && (month > 0 && month < 13);
        Page<Expense> pageResult = lambdaQuery()
                // 按照所有单据，已驳回单据，已审核单据分类
                .eq(expensePageQueryDTO.getStatus()!=null,Expense::getStatus,expensePageQueryDTO.getStatus())
                .eq(expensePageQueryDTO.getLogisticsId() != null, Expense::getLogisticsId, expensePageQueryDTO.getLogisticsId())
                .eq(expensePageQueryDTO.getDriverId() != null, Expense::getDriverId, expensePageQueryDTO.getDriverId())
                .between(expensePageQueryDTO.getBegin() != null && expensePageQueryDTO.getEnd() != null,
                        Expense::getDate, expensePageQueryDTO.getBegin(), expensePageQueryDTO.getEnd())
                .between(flag, Expense::getDate, MonthUtil.getMonthStart(month),MonthUtil.getMonthEnd(month))
                .orderByDesc(Expense::getDate)
                .page(page);
        return new PageResult(pageResult.getTotal(), pageResult.getRecords());
    }

    /**
     * 审核通过
     *
     * @param id
     */
    @Override
    public void approve(Long id) {
        review(id, 1);
    }

    /**
     * 审核拒绝
     *
     * @param id
     */
    @Override
    public void reject(Long id) {
        review(id, 2);
    }


    /**
     * 审核操作
     *
     * @param id
     * @param status
     */
    private void review(Long id, Integer status) {
        Expense expense = getExpense(id);
        expense.setStatus(status); // 更新状态
        expense.setReviewUser(BaseContext.getCurrentId());
        expense.setReviewTime(LocalDateTime.now());
        updateById(expense);
    }

    /**
     * 校验后获取费用实体
     *
     * @param id
     * @return
     */
    private Expense getExpense(Long id) {
        Expense expense = getById(id);
        if (expense == null) {
            throw new ExpenseException("记录不存在");
        }
        if (expense.getStatus() == ReviewStatus.APPROVE.getCode()) {
            throw new ExpenseException("记录审核已通过");
        }
        return expense;
    }


}
